{
"ael_seal": "AEL CS Encyclopedia β Β© Ayman Elmasry",
"owner": "Ayman Elmasry",
"legal_entities": [
"Ayman Elmasry LLC (UAE)",
"Ayman Elmasry Advertising & Marketing (Egypt)"
],
"section": "06_Projects (Week 1 C)",
"syllabus_source": "Harvard CS50x (Projects Architectural Design)",
"methodology": "8-Stage Sub-Silicon Execution Paradigm",
"system_version": "v3.0"
}
The problem sets of Week 1 in C represent the student's primary rite of passage into bare-metal, algorithmically intensive software engineering (Problem-Solving Architecture). In this wing, we deconstruct the underlying algorithmic paradigms and execution strategies for three seminal projects within the CS50x curriculum:
===================================================================================
WEEK 1 C - PROJECTS ARCHITECTURE
===================================================================================
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. MARIO (Less & More Comfortable) β ββ> Nested Loops & Alignment
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 2. CASH (Less Comfortable) β ββ> The Greedy Algorithm
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 3. CREDIT (More Comfortable) β ββ> Luhn's Algorithm & Modulo Math
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
===================================================================================
Inspired by the structural brick blocks of Super Mario Bros, this project requires students to print ascending and descending text-based pyramids in the console terminal. The core architectural goals are mastering the geometric arithmetic of nested loops (Nested Loops) and enforcing strict user input validation (Input Validation).
Construct a single right-aligned pyramid with a user-defined height spanning between 1 and 8. For any zero-indexed row i, leading spaces is spaces = height - i - 1 and hashes is hashes = i + 1.
# (3 spaces, 1 hash) ## (2 spaces, 2 hashes) ### (1 space, 3 hashes) #### (0 spaces, 4 hashes)
Construct two adjacent, opposing pyramids separated by a fixed static gap of exactly two spaces .
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
// 1. Input Validation (Enforce integer between 1 and 8)
do
{
height = get_int("Height: ");
}
while (height < 1 || height > 8);
// 2. Master Loop for Rows
for (int i = 0; i < height; i++)
{
// Print leading spaces
for (int spaces = 0; spaces < height - i - 1; spaces++)
{
printf(" ");
}
// Print left pyramid hashes
for (int hashes = 0; hashes <= i; hashes++)
{
printf("#");
}
// Print middle static gap
printf(" ");
// Print right pyramid hashes
for (int hashes = 0; hashes <= i; hashes++)
{
printf("#");
}
// Move to next row
printf("\n");
}
}
In the Cash problem set, engineers architect an optimized coin-dispensation engine calculated to return the absolute minimum number of coins to a customer.
===================================================================================
THE GREEDY ALGORITHM PIPELINE
===================================================================================
[ Cents Input: e.g., 68Β’ ]
β
βββΊ Quarters (25Β’) ββ> 68 / 25 = 2 (Remainder: 18Β’)
βββΊ Dimes (10Β’) ββ> 18 / 10 = 1 (Remainder: 8Β’)
βββΊ Nickels (5Β’) ββ> 8 / 5 = 1 (Remainder: 3Β’)
βββΊ Pennies (1Β’) ββ> 3 / 1 = 3 (Remainder: 0Β’)
[ Total Minimum Coins = 2 + 1 + 1 + 3 = 7 Coins ]
===================================================================================
A greedy algorithm executes the local optimal decision at each distinct stage of execution, without concern for long-term algorithmic implications. For standard American coin denominations (25Β’, 10Β’, 5Β’, 1Β’), mathematical proofs demonstrate that this greedy heuristic reliably achieves the global optimum.
The Credit problem set represents an advanced engineering challenge requiring the architectural verification of credit card numbers (Visa, MasterCard, American Express) utilizing Luhn's Checksum Algorithm (Luhn's Algorithm).
===================================================================================
LUHN'S CARD VERIFICATION ALGORITHM
===================================================================================
[ Card Number: e.g., 4 0 1 2 8 8 8 8 8 8 8 8 8 8 8 3 ] (Length 16, Starts 4 -> Visa)
β
βββΊ 1. Multiply every second digit from right by 2 (Add product digits)
βββΊ 2. Add sum of digits that weren't multiplied
βββΊ 3. If total modulo 10 == 0, Card is VALID.
===================================================================================
15 digits in length; starts with prefixes 34 or 37.16 digits in length; starts with prefixes spanning 51 through 55.13 or 16 digits in length; starts with prefix 4.2. (If the resulting product yields a two-digit number e.g., 6 * 2 = 12, do not add 12 to the sum. Instead, split the product and sum its individual digits 1 + 2 = 3).total_sum % 10 == 0), the checksum is mathematically validated.Because high-level string manipulation is restricted in early C problem sets, engineers must parse card digits directly from numerical types. By executing modulo division card % 10, the system extracts the trailing rightmost digit. Executing standard division card /= 10 permanently truncates the trailing digit. Given the 16-digit magnitude of credit card numbers, developers must allocate 64-bit long variables to prevent catastrophic overflow (Overflow).